using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common.DataSets;

using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Configuration;

namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.DataAccess
{
    public class MessageType
    {

        #region Get/search methods
        [CLSCompliant(false)]
        public static dsMessageTypeHandler GetMessageTypeHandler()
        {
            dsMessageTypeHandler ds = new dsMessageTypeHandler();
            string SQL = "MessageTypeHandlerSelectAll";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            ds.MessageTypeHandler.Load(db.ExecuteReader(SQL));
            return ds;
        }
        
        /// <summary>
        /// Gets a specific MessageType by ID
        /// </summary>
        /// <param name="MessageTypeID">The id of the MessageType to retrieve</param>
        /// <returns>A typed dataset representing the requested MessageType</returns>
        [CLSCompliant(false)]
        public static dvRefData MessageTypeSelectItem(string messageTypeID)
        {
            dvRefData ds = new dvRefData();
            string SQL = "MessageTypeSelectByID";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            ds.MessageType.Load(db.ExecuteReader(SQL, messageTypeID));
            return ds;
        }


        /// <summary>
        /// Gets all MessageTypes
        /// </summary>
        /// <returns>A typed dataset representing the requested MessageType</returns>
        [CLSCompliant(false)]
        public static dsMessageType MessageTypeSearch(string description, string version, string sourceserviceprovidertypeid, string destinationserviceprovidertypeid)
        {
            dsMessageType ds = new dsMessageType();
            string SQL = "MessageTypeSearch";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());

            //Work Item ID: 127
            //Modified By: SachinJo
            //Checking string length instead of empty string("").
            if (sourceserviceprovidertypeid.Trim().Length == 0) sourceserviceprovidertypeid = null;
            if (destinationserviceprovidertypeid.Trim().Length == 0) destinationserviceprovidertypeid = null;

            ds.MessageType.Load(db.ExecuteReader(SQL, description, version, sourceserviceprovidertypeid, destinationserviceprovidertypeid), LoadOption.OverwriteChanges);

            return ds;
        }

        /// <summary>
        /// Returns true if a MessageType row exists
        /// </summary>
        /// <param name="poolid"></param>
        /// <param name="serviceproviderid"></param>
        /// <returns>True if successful</returns>
        public static int MessageTypeExists(string MessageTypeDescription)
        {  
            
            string SQL = "MessageTypeExists";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            return Convert.ToInt32(db.ExecuteScalar(SQL, MessageTypeDescription));           
        }


        #endregion

        #region Lock/Unlock methods

        /// <summary>
        /// locks a MessageType
        /// </summary>
        /// <param name="MessageTypeid">The MessageType to be locked</param>
        /// <param name="userID">The user that wants to lock the object</param>
        /// <returns>True if successfull)</returns>
        public static bool MessageTypeLock(string messageTypeID, string userID)
        {
            bool result = true;
            string SQL = "MessageTypeLock";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            if (db.ExecuteNonQuery(SQL, messageTypeID, userID) <= 0)
                result = false;

            return result;
        }

        /// <summary>
        /// Unlocks a MessageType
        /// </summary>
        /// <param name="MessageTypeid">The MessageType to be unlocked</param>
        /// <returns>True if successfull)</returns>
        public static bool MessageTypeUnlock(string messageTypeID)
        {
            bool result = true;
            string SQL = "MessageTypeUnlock";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            if (db.ExecuteNonQuery(SQL, messageTypeID) <= 0)
                result = false;

            return result;
        }


        /// <summary>
        /// Unlocks all MessageTypes locked by a user
        /// </summary>
        /// <param name="checkoutproviderid">The user that is unlocking all its' message types</param>
        /// <returns>True if successfull)</returns>
        public static bool MessageTypeUnlockAll(string userID)
        {
            bool result = true;
            string SQL = "MessageTypeUnlockAll";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            db.ExecuteNonQuery(SQL, userID);
                
            return result;
        }


        /// <summary>
        /// Tests if a MessageType is checked out
        /// </summary>
        /// <param name="MessageTypeid">The MessageType to be tested</param>
        /// <returns>True if checked out</returns>
        public static bool MessageTypeIsLocked(string messageTypeID)
        {
            bool result = false;
            string SQL = "MessageTypeIsLocked";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            if (Convert.ToInt32(db.ExecuteScalar(SQL, messageTypeID)) != 0)
                result = true;

            return result;
        }


        /// <summary>
        /// Tests if a MessageType is checked out by a user
        /// </summary>
        /// <param name="MessageTypeid">The MessageType that is to to be tested</param>
        /// <param name="userID">The user that may have the object locked</param>
        /// <returns>True if successful</returns>
        public static bool MessageTypeIsLockedByUser(string messageTypeID, string userID)
        {
            bool result = false;
            string SQL = "MessageTypeIsLockedByUser";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            if (Convert.ToInt32(db.ExecuteScalar(SQL, messageTypeID, userID)) != 0)
                result = true;

            return result;
        }

        #endregion

        #region Delete methods

        /// <summary>
        /// Deletes a MessageType
        /// </summary>
        /// <param name="MessageTypeid">The MessageType to delete</param>
        /// <returns>True if successful</returns>
        public static bool MessageTypeDelete(string messageTypeID)
        {
            bool result = true;
            string SQL = "MessageTypeDelete";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            if (db.ExecuteNonQuery(SQL, messageTypeID) <= 0)
                result = false;

            return result;
        }

        #endregion

        #region  Update/Insert methods

        /// <summary>
        /// Inserts a new MessageType
        /// </summary>
        /// <param name="displayname"></param>
        /// <returns>A new ID if successful, otherwise empty string</returns>
        public static string MessageTypeInsert
        (            
            string description,
            string xmlschema,
            string version
        )
        {
            string result = string.Empty;
            string SQL = "MessageTypeInsert";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            result = db.ExecuteScalar(SQL, description, xmlschema, version).ToString();

            return result;
        }

        /// <summary>
        /// Updates an existing MessageType
        /// </summary>
        /// <param name="MessageTypeid"></param>
        /// <param name="displayname"></param>
        /// <returns>True if successful</returns>
        public static bool MessageTypeUpdate
        (
            string messageTypeID,
            string description,
            string xmlschema,
            string version
        )
        {
            bool result = true;
            string SQL = "MessageTypeUpdate";

            Database db = DatabaseFactory.CreateDatabase(DataAccessHelper.GetDefaultConnectionStringName());
            if (db.ExecuteNonQuery(SQL, messageTypeID, description, xmlschema, version) <= 0)
                result = false;

            return result;
        }

        #endregion
    }
}
